package com.fwtai.service.api;

import com.fwtai.api.ApiContractOrderDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.ToolClient;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

/**
 * 合同模版订单业务层
 */
@Service
public class ApiContractOrderService{

  private final ApiContractOrderDao apiContractOrderDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public ApiContractOrderService(final ApiContractOrderDao apiContractOrderDao,final ThreadPoolTaskExecutor taskExecutor){
    this.apiContractOrderDao = apiContractOrderDao;
    this.taskExecutor = taskExecutor;
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(() -> {
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(apiContractOrderDao.queryById(kid));
    },taskExecutor).join();
  }

  public String delById(final Long kid){
    //若用到ThreadLocal的数据可在此组装
    return CompletableFuture.supplyAsync(() -> {
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.executeRows(apiContractOrderDao.delById(kid),"操作成功","删除失败,刷新重试");
    },taskExecutor).join();
  }
  /**适用于api接口,含数据和总记录数总条数,适用于PC端*/
  //  public String listDataTotal(final HttpServletRequest request,final Integer pageSize){
  //    return CompletableFuture.supplyAsync(()->{
  //      final PageFormData formData = ToolClient.pageParamsApi(request);//此处要做非空验证
  //      if(formData == null) return ToolClient.jsonValidateField();
  //      final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> apiContractOrderDao.getListData(formData),taskExecutor);
  //      final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> apiContractOrderDao.getListTotal(formData),taskExecutor);
  //      CompletableFuture.allOf(listData,listTotal).join();
  //      return ToolClient.paginator(listData.join(),listTotal.join(),pageSize);
  //    },taskExecutor).join();
  //  }

  /**
   * 适用于api接口,仅含数据不含总记录数总条数,前端仅做返回的数据是否小于每页大小
   */
  public String listData(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(() -> {
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      formData.put("loginId",loginId);
      return ToolClient.jsonPage(apiContractOrderDao.getListData(formData));
    },taskExecutor).join();
  }

  /*验证订单存在且有效（模板已被购买）*/
  public boolean queryExist(final HashMap<String,Object> formData){
    final Long kid = apiContractOrderDao.queryExist(formData);
    if(kid == null){
      return false;
    }
    return true;
  }

  public String getListById(final HttpServletRequest request){
    final String loginId = LocalUserId.get();
    return CompletableFuture.supplyAsync(() -> {
      if(loginId == null){
        return ToolClient.accessLogin();
      }
      final PageFormData formData = ToolClient.pageParamsApi(request);
      if(formData == null){
        return ToolClient.jsonValidateField();
      }
      formData.put("userId",loginId);
      return ToolClient.jsonPage(apiContractOrderDao.getListById(formData));
    },taskExecutor).join();
  }
}